package com.zs.studentmanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zs.studentmanager.common.ResponseCodeEnum;
import com.zs.studentmanager.entity.domain.User;
import com.zs.studentmanager.entity.requestParam.LoginParams;
import com.zs.studentmanager.entity.vo.PageVo;
import com.zs.studentmanager.exception.CustomException;
import com.zs.studentmanager.service.UserService;
import com.zs.studentmanager.mapper.UserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
* @author 周小白
* @description 针对表【s_user(系统用户信息表)】的数据库操作Service实现
* @createDate 2024-01-19 19:58:37
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    private final UserMapper userMapper;

    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    /**
     * 登录
     * @param loginParams 登录参数
     * @return User
     */
    @Override
    public User login(LoginParams loginParams) {
        // 参数校验
        if (StringUtils.isBlank(loginParams.getUserName()) ||
                StringUtils.isBlank(loginParams.getPassword())) {
            throw new CustomException(ResponseCodeEnum.NO_PARAM);
        }

        // 查询用户
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserName, loginParams.getUserName());

        return userMapper.selectOne(queryWrapper);
    }


    /**
     * 分页查询
     * @param pageNum 当前页码
     * @param pageSize  每页条数
     * @param searchValue 搜索值
     * @param status 状态
     * @return PageVo
     */
    @Override
    public PageVo<User> getUserPageList(Integer pageNum, Integer pageSize, String searchValue, String status) {
        // 构造分页对象
        Page<User> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(searchValue), User::getRealName, searchValue);
        if (status != null && !Objects.equals("", status) && !Objects.equals("-1", status)) {
            queryWrapper.eq(User::getStatus, Integer.parseInt(status));
        }

        // 分页查询
        Page<User> userPage = userMapper.selectPage(page, queryWrapper);

        PageVo<User> pageVo = new PageVo<>();
        pageVo.setPages(userPage.getPages());
        pageVo.setTotal(userPage.getTotal());
        pageVo.setList(userPage.getRecords());

        return pageVo;
    }
}




